/**
 * AggregationFactory.java
 * ************************** 
 * @date Mar 31, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.aggregation;

import networkRmi.NetworkManager;
import gossipServices.aggregation.functions.AvgAggFunction;
import gossipServices.aggregation.functions.MaximumAggFunction;
import gossipServices.aggregation.functions.MinimunAggFunction;
import gossipServices.basic.view.ConcretePartialView;
import gossipServices.basic.view.PartialView;
import gossipServices.network.NetworkServicesAPI;
import gossipServices.pss.PeerSamplingService;
import gossipServices.pss.PeerSamplingServiceAPI;

/**
 * Description: 
 *
 */
public class AggregationFactory {
	
	protected static long cycleLengthMsec = 1000;
	
	private Aggregation agg;
	private PartialView aggregationView;
	private PeerSamplingServiceAPI pss;
	private NetworkServicesAPI nia;
	
	private AvgAggFunction avgFunc = new AvgAggFunction();
	@SuppressWarnings("unused")
	private MaximumAggFunction maxFunc = new MaximumAggFunction();
	@SuppressWarnings("unused")
	private MinimunAggFunction minFunc = new MinimunAggFunction();
	
	protected double aggStartingValue = 0;
	
	public AggregationFactory(PartialView view, 
			PeerSamplingServiceAPI pss, NetworkManager nm){
		aggregationView = view;
		this.pss = pss;
		this.nia = new AggregationNetworkManager(nm);
	}

	/**
	 * @param view
	 * @param pss2
	 * @param networkManager
	 * @param val
	 */
	public AggregationFactory(ConcretePartialView view,
			PeerSamplingService pss2, NetworkManager networkManager, double val) {
		aggregationView = view;
		this.pss = pss2;
		this.nia = new AggregationNetworkManager(networkManager);
		this.aggStartingValue = val;
		buildAggregationService();		
	}

	public void buildAggregationService(){
		agg = new Aggregation("aggThread", cycleLengthMsec, aggregationView, pss, nia, 
				avgFunc, aggStartingValue);
	}

	public final Aggregation getAgg() {
		return agg;
	}
	
	public boolean peakInitializator(String peakerNodeName, double startingValue){
		boolean ret = true;
		if(nia.getNodeName().equals(peakerNodeName)){
			aggStartingValue = startingValue;
			ret = true;
		}	
		return ret;
		
	}
	
	public boolean buildAggregation(){
		buildAggregationService();
		return true;
	}
		
}
